
*---------------------------------------------------------------------------------------------------*
*                                            OUR ANALYSIS
*                                                               *
*----------------------------------------------------------------------------------------------------*            
cd "J:\gov2001"
* I want to keep parcel taxes in our analysis so I amend and replicate his code from above
use "Source\referenda_newleaids_capital.dta", clear

* fixing year
rename   year yearref
label var yearref "calendar year of elec."
clonevar year=yearref
replace year = year +1 if month>=5
label var year "Spring school year of election"

* Generating classsize reduction dummy variable   
gen cs =(classsize=="Y")
label var cs "bond is for class size reduction"

* drop data not relevant to analysis
keep if cs==1 & year>=1993
 
*Identify measures where percent, passfail, and req dont all match
   gen mv=percent-req_num
   label var mv "voteshare-based MARGIN of VICTORY"
   gen badobs=(mv>=0 & mv<. & passfail=="F")
   replace badobs=1 if mv<0 & passfail=="P"
   replace badobs=1 if mv==.
   count if badobs==1 & percent<.
   count if badobs==1 & percent==.
   replace percent=. if badobs==1
    
 * creat var for bond that wins
   gen win=(passfail=="P") if badobs~=1
 
  * not unique by district and year so I force it to be and prioritize as ordere below
   gsort newleaid year badobs -win -percent 
   by newleaid year: keep if _n==1
   isid newleaid year

 *Form counts of measures per district since 1983
  sort newleaid year
  by newleaid: gen measnum=_n
  by newleaid: gen nummeas=_N
  label var measnum "Measure number (chronological) in district"
  label var nummeas "Number of measures in district in sample"
  tab measnum
  tab nummeas if measnum==nummeas
  su measnum, meanonly
  local maxnmeas=r(max)

 sort newleaid year
*NOTE: this checks whether newleaid and yearref uniquely identify observations in the data
 isid newleaid year
 compress
 gen refid=_n
 drop req
  rename req_num req
 *NOTE: I add the compress
 compress
 drop _merge

* save Data\myreferenda, replace
  
*Make counts of referenda to date and victories to date
gen meastodate=measnum
by newleaid (year): gen winstodate=sum(win)

save "Data\small_referenda.dta", replace


*NOTE: if vars are different dimentions this fills in missing parts of their rectangular matrix so all interactions in the data set
fillin newleaid year
sort newleaid year
by newleaid: replace meastodate=0 if _n==1 & _fillin
by newleaid: replace winstodate=0 if _n==1 & _fillin
by newleaid: replace meastodate=meastodate[_n-1] if meastodate==.
by newleaid: replace winstodate=winstodate[_n-1] if winstodate==.
drop _fillin
sort newleaid year


 
*--------------------------------------------------------------------------------------*
*
*                         Merging on Ed Data outcomes to entire panel
*---------------------------------------------------------------------------------------*

** destrining district_code
sort newleaid
destring district_code, replace
bysort newleaid: egen new_discode = max(district_code)
replace district_code=new_discode
sort district_code year
merge m:1 district_code year using "Source\dist_outcome.dta" 
tab year _merge
drop if _merge==2
drop _merge
compress
** we have matched outcomes from 1993 to 2006!!!!!
* note we are not matching about 20-30 observations of 1554 per year in the data.  Not sure why



 
*--------------------------------------------------------------------------------------*
*
*                         Merging on academic achievement outcomes to entire panel
*---------------------------------------------------------------------------------------*

capture drop _merge
sort district_code year
merge m:1 district_code year using Source\ca_allyrs_reduced
tab year _merge 
*drop if _merge==2
drop _merge

** gen vars for gobond and parcel
gen bond = (election_type=="GO Bond")
gen parcel = (election_type=="Parcel Tax")

 save Data\classsize, replace
*--------------------------------------------------------------------------------------*
*
*                         creating variables for analysis
*---------------------------------------------------------------------------------------*
use "Data\classsize", clear
  

*twoway (scatter classsize_1 mv)(lpoly classsize_1 mv if mv<0)(lpoly classsize_1 mv if mv>0)
*twoway (scatter averageclasssize mv)(lpoly averageclasssize mv if mv<0)(lpoly averageclasssize mv if mv>0)
*twoway (scatter avdiff_1 mv ,ylabel(-5 (.1) 5))(lpoly avdiff_1 mv if mv<0 ,ylabel(-5 (.1) 5))(lpoly avdiff_1 mv if mv>0,ylabel(-5 (.1) ) )
**  creating vars for regressions **
tab year, gen(yrdums)
*Make squared, cubed, etc. vars
 gen percent2=percent^2
 gen percent3=percent^3	
 gen mv2=mv^2
 gen mv3=mv^3
 gen mv4=mv^4
gen win_mv=win*mv
gen win_mv2=win*mv2
gen win_mv3=win*mv3
gen win_mv4=win*mv4

*--------------------------------------------------------------------------------------*
*
*                         OUR FINAL MODELS FOR THE PAPER 
*---------------------------------------------------------------------------------------*
sort newleaid year
foreach var of varlist averageclasssize{
by newleaid: gen classsize_m2 =  `var'[_n-2]
by newleaid: gen classsize_m1 =  `var'[_n-1]
by newleaid: gen classsize_n1 =  `var'[_n+1]
by newleaid: gen classsize_n2 =  `var'[_n+2]
by newleaid: gen classsize_n3 =  `var'[_n+3]
by newleaid: gen classsize_n4 =  `var'[_n+4]
by newleaid: gen classsize_n5 =  `var'[_n+5]
by newleaid: gen classsize_n6 =  `var'[_n+6]   
}

                                                 **** our discontinuity graph!!!!!!!!!!!!!!!! FIGURE II ***

twoway (lpoly classsize_m1 mv if bond==1 & mv>=-10 & mv<=10 label)(lpoly classsize_n4 mv if bond==1 & mv>=-10 & mv<=10)

gen mvr = round(mv,2)
sort mvr
bysort mvr: egen avg_m1 = mean(classsize_m1) if bond==1
bysort mvr: egen avg_n3 = mean(classsize_n3) if bond==1

*drop if mvr==0
set scheme s2mono
# delimit ;
twoway (connected avg_m1 mvr if bond==1 & mv>=-10 & mv<=10, clpattern(dash solid) xtitle("Vote share relative to threshold (2 %point bins)")
          ytitle("Average District Classsize") legend(label(1 "Year before election") label(2 "Three years after election")))
		  (connected avg_n3 mvr if bond==1 & mv>=-10 & mv<=10, title("Average Class Size, by Margin of Victory"
              "One Year Before and Three Years After Bond Referenda"));
		  
		  
drop mvr avg_m1 

*** Generating difference in class size ***
gen csdiff_m1 = classsize_m1 - classsize_m2
gen csdiff_0 = averageclasssize - classsize_m1
gen csdiff_1 = classsize_n1 - classsize_m1
gen csdiff_2 = classsize_n2 - classsize_m1
gen csdiff_3 = classsize_n3 - classsize_m1
gen csdiff_4 = classsize_n4 - classsize_m1
gen csdiff_5 = classsize_n5 - classsize_m1
gen csdiff_6 = classsize_n6 - classsize_m1

gen avdiff_1 = classsize_n1 - averageclasssize
gen avdiff_2 = classsize_n2 - averageclasssize
gen avdiff_3 = classsize_n3 - averageclasssize
gen avdiff_4 = classsize_n4 - averageclasssize
gen avdiff_5 = classsize_n5 - averageclasssize
gen avdiff_6 = classsize_n6 - averageclasssize

**** using difference in class size variables
gen pre_csr=(year<=1996)
gen post_csr=(year>=1999)

* bond
estimates clear
foreach var of varlist csdiff_m1 csdiff_1 csdiff_2 csdiff_3 csdiff_4 csdiff_5 csdiff_6{
 eststo: reg `var' win percent percent2 percent3 pre_csr post_csr req if bond==1 
}
estout using Output\ITT_gain_classsize_gobond.rtf, cells(b(star fmt(3)) se(fmt(2))) legend label title(Regression Models) mlabels("m1" "1" "2" "3" "4" "5" "6") varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)

*parcel
estimates clear
foreach var of varlist csdiff_m1 csdiff_1 csdiff_2 csdiff_3 csdiff_4 csdiff_5 csdiff_6{
 eststo: reg `var' win percent percent2 percent3 pre_csr post_csr req if parcel==1 
}
estout using Output\ITT_gain_classsize_parcel.rtf, cells(b(star fmt(3)) se(fmt(2))) legend label title(Regression Models) mlabels("m1" "1" "2" "3" "4" "5" "6") varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)

* any measure
estimates clear
foreach var of varlist csdiff_m1 csdiff_1 csdiff_2 csdiff_3 csdiff_4 csdiff_5 csdiff_6{
 eststo: reg `var' win percent percent2 percent3 pre_csr post_csr req 
 }
estout using Output\ITT_gain_classsize_anymeasure.rtf, cells(b(star fmt(3)) se(fmt(2))) legend label title(Regression Models) mlabels("m1" "1" "2" "3" "4" "5" "6") varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)




estout using Output\ITT_gain_classsize.rtf, cells(b(star fmt(3)) se(par fmt(2))) legend label title(Regression Models) mlabels("m2" "m1" "1" "2" "3" "4" "5" "6") varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)



estimates clear
foreach var of varlist avdiff_1 avdiff_2 avdiff_3 avdiff_4 avdiff_5 {
eststo: reg `var' win mv mv2 mv3 req pre_csr post_csr if gobond==1 
}
 
*** Generating difference in class size ***
gen diff31 = std_ms_score_31_n1 - std_ms_score_31_m1 
gen diff32 = std_ms_score_32_n1 - std_ms_score_32_m1 
gen diff71 = std_ms_score_71_n1 - std_ms_score_71_m1 
gen diff72 = std_ms_score_72_n1 - std_ms_score_72_m1 

estimates clear
foreach var of varlist avdiff31 diff32 diff71 diff72 {
quietly: eststo: areg `var' win mv mv2 mv3 win_mv win_mv2 win_mv3 winstodate meastodate req, absorb(year) cluster(newleaid)
}
estout using Output\ITT_gain_testscore.rtf, cells(b(star fmt(3)) se(par fmt(2))) legend label title(Regression Models) mlabels("31" "32" "71" "72" ) varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)


gen diff_not_1 = numberofteachers_n1- numberofteachers_m1 
gen diff_not_2 = numberofteachers_n2- numberofteachers_m1 
gen diff_exp_1 =  twoyrsexperience_n2 -  twoyrsexperience_m1
gen diff_exp_2 = twoyrsexperience_n2 -  twoyrsexperience_m1
 
estimates clear
foreach var of varlist  diff_not_1 diff_not_2 diff_exp_1 diff_exp_2 {
eststo: areg `var' win mv mv2 mv3 win_mv win_mv2 win_mv3 winstodate meastodate req, absorb(year) cluster(newleaid)
}


gen diff31_3 = std_ms_score_31_n3 - std_ms_score_31_m1 
gen diff32_3 = std_ms_score_32_n3 - std_ms_score_32_m1 
gen diff71_3 = std_ms_score_71_n3 - std_ms_score_71_m1 
gen diff72_3 = std_ms_score_72_n3 - std_ms_score_72_m1 

foreach var of varlist diff31_3 diff32_3 diff71_3 diff72_3 {
eststo: reg `var' win percent percent2 percent3 req pre_csr post_csr if gobond==1 
}
save Data/simple_forgraph.dta, replace


*--------------------------------------------------------------------------------------------------------*
*                                             Descriptive Stats                                          *
*--------------------------------------------------------------------------------------------------------*


* median values for districts that propose classsize bonds

set more off
tempname myfile1 
file open `myfile1' using "Output\decriptives_parcel_gobond_both.txt", write text replace
file write `myfile1' _tab "Median Values"  _n 
file write `myfile1' _tab "Propose any Measure" _tab "Propose bond" _tab "Propose Parcel" _n 
file write `myfile1' "Sample Size" _tab "167" _tab "43" _tab "133" _n 
foreach v of varlist  std_ms_score_31 std_ms_score_32 std_ms_score_71 std_ms_score_72 averageclasssize averagedailyattenada expendituresperada englishlearners freeorreducedmeals minority ethnicdiversityindex  numberofteachers numberoffulltimeequivalents fullycredentialled  pupilsperteacher averageteachersalary gradsuccsucourses yeardropoutrate twoyrsexperience ba60salary proficientlangarts proficientmath adjusted1yeardropoutrate   {
	quietly: sum (`v') if win~=. , d
	local a = r(p50)
	quietly: sum (`v') if bond==1 , d
	local b = r(p50)
	quietly: sum (`v') if parcel==1 , d
	local c = r(p50)
	file write `myfile1' "`v'" _tab "`a'" _tab "`b'" _tab "`c'" _n
            }		

*** graph of state trends  vs bond passage trends
sort year
bysort year: egen cs_med=median(averageclasssize)
bysort year: egen cs_med_bond=median(averageclasssize) if win~=.

*twoway (connected cs_med_bond year if year<=2007, xlabel(1992 (2) 2008) ylabel(22 (1) 28) ytitle("Median Classsize"))(connected cs_med year if year<=2007, xlabel(1992 (2) 2008) ylabel(22 (1) 28) ytitle("Median Classsize"))

* showing variables are discontinuous			
twoway (scatter averagedailyattenada_m1 mv if averagedailyattenada_m1<200000 y)(lpoly averagedailyattenada_m1 mv if mv<0 & averagedailyattenada_m1<200000)(lpoly averagedailyattenada_m1 mv if mv>0 & averagedailyattenada_m1<200000)
twoway (scatter std_ms_score_31_m1 mv if averagedailyattenada_m1<200000)(lpoly std_ms_score_31_m1 mv if mv<0 & averagedailyattenada_m1<200000)(lpoly std_ms_score_31_m1 mv if mv>0 & averagedailyattenada_m1<200000)
twoway (scatter std_ms_score_32_m1 mv if averagedailyattenada_m1<200000)(lpoly std_ms_score_32_m1 mv if mv<0 & averagedailyattenada_m1<200000)(lpoly std_ms_score_32_m1 mv if mv>0 & averagedailyattenada_m1<200000)
twoway (scatter std_ms_score_32_n3 mv if averagedailyattenada_m1<200000)(lpoly std_ms_score_32_n3 mv if mv<0 & averagedailyattenada_m1<200000)(lpoly std_ms_score_32_n3 mv if mv>0 & averagedailyattenada_m1<200000)
twoway (scatter std_ms_score_71_m1 mv if averagedailyattenada_m1<200000)(lpoly std_ms_score_71_m1 mv if mv<0 & averagedailyattenada_m1<200000)(lpoly std_ms_score_71_m1 mv if mv>0 & averagedailyattenada_m1<200000)
twoway (lpoly std_ms_score_72_m1 mv if averagedailyattenada_m1<200000)(lpoly std_ms_score_72_m1 mv if mv<0 & averagedailyattenada_m1<200000)(lpoly std_ms_score_72_m1 mv if mv>0 & averagedailyattenada_m1<200000)
			
			
			
** T-tests of classize difference a year before bond election
ttest classsize_m1 if mv>=-1 & mv<=1, by(win)
ttest classsize_m1 if mv>=-2 & mv<=2, by(win)
ttest classsize_m1 if mv>=-3 & mv<=3, by(win)

ttest classsize_3 if mv>=-1 & mv<=1, by(win)
ttest classsize_3 if mv>=-2 & mv<=2, by(win)
ttest classsize_3 if mv>=-3 & mv<=3, by(win)

*** T-tests of district student performace pre post elections ***

foreach var of varlist  std_ms_score_31 std_ms_score_32 std_ms_score_71 std_ms_score_72 {
forvalues n=1/3 {
ttest `var'_m1 if mv>=-`n' & mv<=`n', by(win)
               }
		}
	
foreach var of varlist  std_ms_score_31 std_ms_score_32 std_ms_score_71 std_ms_score_72 {
forvalues n=1/3 {
ttest `var'_n3 if mv>=-`n' & mv<=`n', by(win)
               }
		}		

		
** descritives  -/+ 3 two percent
set more off
tempname myfile1 
file open `myfile1' using "Output\decriptives_3percent.txt", write text replace
file write `myfile1' _tab "Bond on Ballot" _tab "Pass Bond" _tab "Reject Bond" _tab "Sig Diff" _n 
foreach v of varlist std_ms_score_31_m1 std_ms_score_32_m1 std_ms_score_71_m1 std_ms_score_72_m1 classsize_m1 averagedailyattenada_m1 expendituresperada_m1 englishlearners_m1 freeorreducedmeals_m1 minority_m1 ethnicdiversityindex_m1 numberofteachers_m1 numberoffulltimeequivalents_m1 fullycredentialled_m1 pupilsperteacher_m1 averageteachersalary_m1 gradsuccsucourses_m1 yeardropoutrate_m1 twoyrsexperience_m1 ba60salary_m1 proficientlangarts_m1 proficientmath_m1 {
	quietly: sum (`v') if win~=.
	local a = r(mean)
	quietly: sum(`v') if win==1 & mv>=-3 & mv<=3
	local b = r(mean)
	quietly: sum(`v') if win==0 & mv>=-3 & mv<=3
	local c = r(mean)
	quietly: ttest `v' if mv>=-3 & mv<=3, by(win)
	local d = r(p)
	file write `myfile1' "`v'" _tab "`a'" _tab "`b'" _tab "`c'" _tab "`d'" _n
            }
		
** descritives  -/+ 1 two percent
set more off
tempname myfile1 
file open `myfile1' using "Output\decriptives_1percent.txt", write text replace
file write `myfile1' _tab "Bond on Ballot" _tab "Pass Bond" _tab "Reject Bond" _tab "Sig Diff" _n 
foreach v of varlist classsize_m1 std_ms_score_31_m1 std_ms_score_32_m1 std_ms_score_71_m1 std_ms_score_72_m1 ba60salary_m1 twoyrsexperience_m1 yeardropoutrate_m1 gradsuccsucourses_m1 averageteachersalary_m1 fullycredentialled_m1 numberoffulltimeequivalents_m1 numberofteachers_m1 enrollment_m1 averagedailyattenada_m1 expendituresperada_m1 englishlearners_m1 freeorreducedmeals_m1 minority_m1 ethnicdiversityindex_m1 {
	quietly: sum (`v')
	local a = r(mean)
	quietly: sum(`v') if win==1 & mv>=-1 & mv<=1
	local b = r(mean)
	quietly: sum(`v') if win==0 & mv>=-1 & mv<=1
	local c = r(mean)
	quietly: ttest `v' if mv>=-1 & mv<=1, by(win)
	local d = r(p)
	file write `myfile1' "`v'" _tab "`a'" _tab "`b'" _tab "`c'" _tab "`d'" _n
            }
		
** Post 3 years descritives  -/+ 3 two percent
set more off
tempname myfile1 
file open `myfile1' using "Output\Post_decriptives_3percent.txt", write text replace
file write `myfile1' _tab "Bond on Ballot" _tab "Pass Bond" _tab "Reject Bond" _tab "Sig Diff" _n 
foreach v of varlist classsize_3 std_ms_score_31_n3 std_ms_score_32_n3 std_ms_score_71_n3 std_ms_score_72_n3 ba60salary_n3 twoyrsexperience_n3 yeardropoutrate_n3 gradsuccsucourses_n3 averageteachersalary_n3 fullycredentialled_n3 numberoffulltimeequivalents_n3 numberofteachers_n3 enrollment_n3 averagedailyattenada_n3 expendituresperada_n3 englishlearners_n3 freeorreducedmeals_n3 minority_n3 ethnicdiversityindex_n3 {
	quietly: sum (`v')
	local a = r(mean)
	quietly: sum(`v') if win==1 & mv>=-3 & mv<=3
	local b = r(mean)
	quietly: sum(`v') if win==0 & mv>=-3 & mv<=3
	local c = r(mean)
	quietly: ttest `v' if mv>=-3 & mv<=3, by(win)
	local d = r(p)
	file write `myfile1' "`v'" _tab "`a'" _tab "`b'" _tab "`c'" _tab "`d'" _n
            }		
		
	
		
*OUR ITT MODELS
estimates clear
foreach var of varlist classsize_m2 classsize_m1 classsize_1 classsize_2 classsize_3 classsize_4 classsize_5 {
 areg `var' win mv mv2 mv3 win_mv win_mv2 win_mv3 winstodate meastodate req, absorb(year) cluster(newleaid)
}
estout using Output\ITT_classsize.rtf, cells(b(star fmt(3)) se(par fmt(2))) legend label title(Regression Models) mlabels("m2" "m1" "1" "2" "3" "4" "5") varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)

* robust to quartic!!!!
areg classsize_3 win mv mv2 mv3 mv4 win_mv win_mv2 win_mv3 win_mv4 winstodate meastodate req, absorb(year) cluster(newleaid)

areg classsize_3 win mv mv2 mv3 win_mv win_mv2 win_mv3 winstodate meastodate req, absorb(year) cluster(newleaid)

* controlling for previous classsize
estimates clear
foreach var of varlist classsize_1 classsize_2 classsize_3 classsize_4 classsize_5 {
quietly: eststo: areg `var' classsize_m1 win mv mv2 mv3 win_mv win_mv2 win_mv3 winstodate meastodate req, absorb(year) cluster(newleaid)
}
estout using Output\ITT_classsize_lagg.rtf, cells(b(star fmt(3)) se(par fmt(2))) legend label title(Regression Models) mlabels("1" "2" "3" "4" "5") varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)




                                                *** test scores  ***
estimates clear
set more off
foreach var of varlist std_ms_score_31 std_ms_score_32 std_ms_score_71 std_ms_score_72 {
          	quietly: eststo:areg `var'_n1 win mv mv2 mv3 mv4 win_mv win_mv2 win_mv3 win_mv4 winstodate meastodate req, absorb(year) cluster(newleaid)
	  		quietly: eststo:areg `var'_n1 `var'_m1 win mv mv2 mv3 mv4 win_mv win_mv2 win_mv3 win_mv4 winstodate meastodate req, absorb(year) cluster(newleaid)
				}
estout using Output\ITT_test_1.rtf, cells(b(star fmt(3)) se(par fmt(2))) legend label title(Regression Models) mlabels("1" "2" "3" "4" "5") varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)
			
estimates clear
set more off
foreach var of varlist std_ms_score_31 std_ms_score_32 std_ms_score_71 std_ms_score_72 {
          	quietly: eststo:areg `var'_n3 win mv mv2 mv3 mv4 win_mv win_mv2 win_mv3 win_mv4 winstodate meastodate req, absorb(year) cluster(newleaid)
	  		quietly: eststo:areg `var'_n3 `var'_m1 win mv mv2 mv3 mv4 win_mv win_mv2 win_mv3 win_mv4 winstodate meastodate req, absorb(year) cluster(newleaid)
				}
estout using Output\ITT_test_3.rtf, cells(b(star fmt(3)) se(par fmt(2))) legend label title(Regression Models) mlabels("1" "2" "3" "4" "5") varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)
			
				
			
			                                           **** locally linear ****
estimates clear
set more off
foreach var of varlist avdiff_1 avdiff_2 avdiff_3 avdiff_4 avdiff_5 {
          	areg `var' win mv win_mv winstodate meastodate req if mv>=-4 & mv<=4, absorb(year) cluster(newleaid)
				}
estout using Output\local_linear.rtf, cells(b(star fmt(3)) se(par fmt(2))) legend label title(Regression Models) mlabels("1" "2" "3" "4" "5") varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)
																   
			
			
			
                                           *** with lagged test score controls ***
set more off
foreach var of varlist std_ms_score_31 std_ms_score_32 std_ms_score_71 std_ms_score_72 {
          forvalues n=1/4 {
		  	areg `var'_n`n' `var'_m1 win mv mv2 mv3 mv4 win_mv win_mv2 win_mv3 win_mv4 winstodate meastodate req, absorb(year) cluster(newleaid)
	  				}
			}
			
			                                 ***** other outcomes of interest ******
						 					 
estimates clear
foreach var of varlist ba60salary twoyrsexperience yeardropoutrate gradsuccsucourses averageteachersalary fullycredentialled numberoffulltimeequivalents numberofteachers averagetotalsatscore ofseniorstestedforsat enrollment {
quietly: eststo: areg `var'_n3 win mv mv2 mv3 win_mv win_mv2 win_mv3 winstodate meastodate req, absorb(year) cluster(newleaid)
}
estout using Output\ITT_otheroutcomes_3.rtf, cells(b(star fmt(3)) se(par fmt(2))) legend label title(Regression Models) mlabels("1" "2" "3" "4" "5") varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)

											 
** salary looks to be the only variable of interest
sort newleaid year
foreach var of varlist averageteachersalary ba60salary {
by newleaid: gen `var'_m1 =  `var'[_n-1]
by newleaid: gen `var'_n1 =  `var'[_n+1]
by newleaid: gen `var'_n2 =  `var'[_n+2]
by newleaid: gen `var'_n3 =  `var'[_n+3]
            }
			
foreach var of varlist averageteachersalary ba60salary {
forvalues n=1/3 {
ttest `var'_m1 if mv>=-`n' & mv<=`n', by(win)
               }
		}
		
foreach var of varlist averageteachersalary ba60salary {
forvalues n=1/3 {
ttest `var'_n3 if mv>=-`n' & mv<=`n', by(win)
               }
		}
		
				
								
																
																																
																																																																									 

*-------------------------------------------------------------------------------------------------------------------*
*                         ** Experimenting with Two stage model with IV
*--------------------------------------------------------------------------------------------------------------
estimates clear
foreach var of varlist std_ms_score_31 std_ms_score_32 std_ms_score_71 std_ms_score_72 {
          forvalues n=1/4 {
			quietly: eststo: xi: ivreg `var'_n`n' `var'_m1 mv mv2 mv3 win_mv win_mv2 win_mv3 winstodate req i.year (classsize_`n' = win), cluster(newleaid) first
				}
			}
estout using Output\2SLS_lagg.rtf, cells(b(star fmt(3)) se(par fmt(2))) legend label title(Regression Models) mlabels("1" "2" "3" "4" "5") varlabels(_cons INTERCEPT) stats(N r2 df_r, fmt(0 3 0) label (N R2 DF)) style(fixed)

                                      ************ using gain scores ****************
*** Generating difference in class size ***
gen diff31_3 = std_ms_score_31_n3 - std_ms_score_31_m1 
gen diff32_3 = std_ms_score_32_n3 - std_ms_score_32_m1 
gen diff71_3 = std_ms_score_71_n3 - std_ms_score_71_m1 
gen diff72_3 = std_ms_score_72_n3 - std_ms_score_72_m1 
																		  
foreach var of varlist diff31_3 diff32_3 diff71_3 diff72_3 {
          xi: ivreg `var' percent percent2 percent3 winstodate req pre_csr post_csr  (csdiff_3 = win) if gobond==1, first
				}
		












* falsification check
areg d_std_ms_score_71_m1 win mv mv2 mv3 mv4 win_mv win_mv2 win_mv3 win_mv4  winstodate meastodate req, absorb(year) cluster(newleaid)
	
						
twoway (scatter d_std_ms_score_72_n3 mv)(lpoly d_std_ms_score_72_n3 mv if mv<0)(lpoly d_std_ms_score_72_n3 mv if mv>0)
twoway (scatter d_std_ms_score_72_m1 mv)(lpoly d_std_ms_score_72_m1 mv if mv<0)(lpoly d_std_ms_score_72_m1 mv if mv>0)

						

gen class_3ydiff = classsize_3 -averageclasssize 
gen laggdiff = classsize_3 - classsize_m1				
mean (classsize_3), over(win)		
mean (averageclasssize)if classsize_3~=., over(win)		
mean laggdiff , over(win)
	
mean( d_std_ms_score_71) if win~=.
	
	
	
* ------------------------------------------------------------* 
*                    attempting rothstein model              *
* --------------------------------------------------------------*
use Data\small_referenda.dta, clear
isid newleaid year
keep newleaid year
save "Data\small_referenda.dta" ,replace

use Data\classsize
merge m:1 
/*
gen year2 = year if win~=.
replace year2=0 if year2==.
label var year "Spring school year of election"
sort newleaid year
bysort newleaid: egen temp = max(year2) 
*/

 *Make leads and lags
  foreach v in percent percent2 percent3 win req {
   forvalues dy=1/19 {
    gen `v'_m`dy'=`v'*(dyear==-`dy')
    }
   forvalues dy=0/19 {
    gen `v'_`dy'=`v'*(dyear==`dy')
   }
   *drop `v'
  }

 keep if dyear>=-2 & dyear<=6
 *Drop other variables
  foreach v in percent percent2 percent3 win req {
    forvalues y=7/19 {
      drop `v'_`y'
      }
    forvalues y=3/19 {
      drop `v'_m`y'
      }
    }
